package cn.dengta.webapp.im.entity;

import java.util.Date;

import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 速配记录表
 *
 * @author Ready
 * @since 2022-07-22
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_speed_dating_log")
public class SpeedDatingLog extends TimeBasedEntity {

	private static final long serialVersionUID = 1L;

	public static final Integer
			STATUS_CANCEL = -2,
			STATUS_REJECT = -1,
			STATUS_INIT = 0,
			STATUS_OK = 1;

	/** 速配类型：2=语音；3=视频 */
	Integer type;
	/** 主叫用户ID */
	Long fromUserId;
	/** 男用户ID */
	Long maleUserId;
	/** 男用户是否在速配池中 */
	Integer maleInside;
	/** 女用户ID */
	Long femaleUserId;
	/** 女用户是否在速配池中 */
	Integer femaleInside;
	/** 男用户加入速配时间 */
	Date maleJoinTime;
	/** 频道号 */
	String channelName;
	/** 通话时长（单位：秒） */
	Integer duration;
	/** 消费钻石数 */
	Integer cost;
	/** 礼物ID */
	Long giftId;
	/** 状态：-1=已拒接；0=未接听；1=已接听 */
	Integer status;
	/** 状态 */
	Integer state;

	/** 速配次数 */
	public transient Integer speedTimes;
	/** 接通次数 */
	public transient Integer callCount;
	/** 速配人数 */
	public transient Integer speedUsers;
	public transient SpeedDatingQueue from;
	public transient SpeedDatingQueue to;
	/** 当前是否为破冰模式 */
	public transient boolean iceBreaking;

	public SpeedDatingLog init(Integer type, boolean fromMale, SpeedDatingQueue maleQueue, SpeedDatingQueue femaleQueue, String channelName, Date now) {
		this.type = type;
		this.from = fromMale ? maleQueue : femaleQueue;
		this.to = fromMale ? femaleQueue : maleQueue;
		this.fromUserId = from.getUserId();
		this.maleUserId = maleQueue.getUserId();
		this.maleInside = maleQueue.getInside();
		this.femaleUserId = femaleQueue.getUserId();
		this.femaleInside = femaleQueue.getInside();
		this.channelName = channelName;
		this.duration = 0;
		this.cost = 0;
		this.status = 0;
		this.initTime(now);
		return this;
	}

	public Long findToUserId(Long userId) {
		return userId.equals(maleUserId) ? femaleUserId : maleUserId;
	}

	public boolean bothInside() {
		return maleInside == 1 && femaleInside == 1;
	}

	/**
	 * 是否已送礼
	 */
	public Boolean hasSendGift(){
		return giftId != null;
	}
	public Long targetUserId() {
		return fromUserId.longValue() == maleUserId.longValue() ? femaleUserId : maleUserId;
	}

	public static final String TYPE = "type";
	public static final String MALE_USER_ID = "male_user_id";
	public static final String FEMALE_USER_ID = "female_user_id";
	public static final String CHANNEL_NAME = "channel_name";
	public static final String DURATION = "duration";
	public static final String COST = "cost";
	public static final String GIFT_ID = "gift_id";
	public static final String STATUS = "status";
	public static final String STATE = "state";

}
